<?php
/**
 * 卡相关接口
 */
namespace App\Controller;

use Think\Controller;

class CardController extends Controller {
    private $m_card;    //卡model
    private $m_recharge;

    public function _initialize() {
	    $this->m_card = D("Admin/Card");
        $this->m_recharge = D("Admin/Recharge");
    }

    public function detail() {
        $card_number = I("post.card_number");
        if(empty($card_number)){
            $this->errorReturn('卡号不能为空');
        }
        $where = [
            'number'=>$card_number,
        ];
        $card = $this->m_card->get_one($where,'c.id,c.lid,c.balance,c.is_active,l.logistics_name');
        if($card){
            $this->successReturn($card);
        }else{
            $this->errorReturn('该卡号不存在');
        }
    }

    /**
     * 创建电子卡
     */
    public function create(){
        $card_number = I("post.card_number");   //卡号
        $lid = I("post.lid",0);   //物流公司ID

        if(empty($card_number)){
            $this->errorReturn('卡号不能为空');
        }
        $result = $this->m_card->add_e_card($card_number,$lid);
        switch($result){
            case -1:
                $this->errorReturn('卡号已存在');
                break;
            case -2:
                $this->errorReturn('物流公司不存在');
                break;
            case -3:
                $this->errorReturn('创建失败');
                break;
            default:
                $this->successReturn(['card_number'=>$result]);
        }
    }


    /**
     * 卡充值
     */
    public function recharge(){
        $card_number = I("post.card_number");
        $price = I("post.recharge_price",0);
        $desc = I("post.desc");
        $payment_model = I("post.payment_model");
        if(empty($card_number)){
            $this->errorReturn('卡号不能为空');
        }
        if(empty($price) || !is_numeric($price) || $price<=0){
            $this->errorReturn('金额输入有误');
        }
        $card = $this->m_card->where(array('number'=>$card_number))->find();

        if(empty($card)){
            $this->errorReturn('卡号不存在');
        }
        if($card['card_type'] != 1){
            $this->errorReturn('目前只支持给电子卡充值');
        }
        if(!in_array($payment_model,[1,2,3])){
            $this->errorReturn('支付方式有误！');
        }

        $result =  $this->m_card->app_pre_recharge($card_number,$price,$desc,$payment_model);
        if($result && isset($result['number'])){
            $data = [
                'card_number'=>$card_number,
                'order_number'=>$result['number']
            ];
            $this->successReturn($data);
        }else{
            $this->errorReturn('订单提交失败');
        }

    }

    /**
     * 回调
     */
    public function recharge_call_back(){
        $card_number = I("post.card_number");
        $price = I("post.recharge_price",0);
        $order_number = I("post.order_number",0);

        if(empty($card_number)){
            $this->errorReturn('卡号不能为空');
        }
        if(empty($price) || !is_numeric($price) || $price<=0){
            $this->errorReturn('金额有误');
        }

        if(empty($order_number)){
            $this->errorReturn('订单号不能为空');
        }


        $result =  $this->m_recharge->app_update_status($card_number,$price,$order_number);
        switch($result){
            case -1:
                $this->errorReturn('卡不存在');
                break;
            case -2:
                $this->errorReturn('充值记录不存在');
                break;
            case -3:
                $this->errorReturn('充值金额有误');
                break;
            case -4:
                $this->errorReturn('充值失败');
                break;
            case is_array($result):
                $this->successReturn(['card_number'=>$card_number,'after_balance'=>$result['after_balance']]);
                break;
            default:
                $this->errorReturn('充值失败');
                break;
        }
    }

}

